package com.ibm.spatiotemp.test.requestServer;

import java.io.IOException;
import java.text.ParseException;
import java.util.Map;

import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ibm.spatiotemp.server.config.LocalConfig;
import com.ibm.spatiotemp.server.framework.handler.DataRequestHandler;
import com.ibm.spatiotemp.server.framework.handler.RequestHandlerManager;
import com.ibm.spatiotemp.server.framework.request.Request;
import com.ibm.spatiotemp.test.requestServer.factory.ResponseSenderFactory;
import com.ibm.spatiotemp.tools.jsonParser;
import com.ibm.spatiotemp.utils.StringEncoder;
/**
 * a mocking requesthandler
 * @author xuxiaomin
 *
 */
public class RequestHandler implements Runnable{
	
	public static Logger logger = Logger.getLogger(RequestHandler.class);
	
	ResponseSender response_sender;
	
	String respservip;
	String respport;
	String resptopic;
	String request_paras;
	/* json format request */
	public RequestHandler(String req) {
		JsonObject reqobj = new JsonObject(req);
		request_paras = reqobj.getAttributeAsString(JsonObject.RequestTags[0]);
		resptopic = reqobj.getAttributeAsString(JsonObject.RequestTags[1]);
		respservip = reqobj.getAttributeAsString(JsonObject.RequestTags[2]);
		respport = reqobj.getAttributeAsString(JsonObject.RequestTags[3]);
		response_sender = ResponseSenderFactory.getResponseSenderInstance(respservip,respport);
	}

	public String HandleRequest(String requestparas) {
		logger.info("test handle request:" + requestparas);
		ObjectNode jsnode = jsonParser.newObjectNode();
		
		jsnode.put(JsonObject.ResponseTags[0], resptopic);
		String respstring = handleRequest_internal(requestparas);
		jsnode.put(JsonObject.ResponseTags[1], respstring);
		return jsnode.toString();
	}
	
	/**4 demo requests 
	 * http://127.0.0.1:8080/demo/handlequeryservlet?tenantid=mocktenant&sourceid=mocksrc&start=2014-01-01T00:00:51&end=2014-01-01T00:01:11&sourceid=mocksrc&id=2&queryType=trajectory
	 * http://127.0.0.1:8080/demo/handlequeryservlet?tenantid=mocktenant&sourceid=mocksrc&start=2014-01-01T00:00:45&end=2014-01-01T00:01:05&sourceid=mocksrc&bboxRel=intersect&bbox=[140.323171,20.880869,160.323171,40.880869]&queryType=trajectory
	 * http://127.0.0.1:8080/demo/handlequeryservlet?tenantid=mocktenant&sourceid=mocksrc&sourceid=mocksrc&bbox=[139.658454,22.546554,159.658454,42.546554]&time=2014-01-01T00:00:39&queryType=position
	 * http://127.0.0.1:8080/demo/handlequeryservlet?tenantid=mocktenant&sourceid=mocksrc&start=2014-01-01T00:00:40&end=2014-01-01T00:01:00&sourceid=mocksrc&distance=5&id=2&position=[149.771018,32.072983]&queryType=firsttime
	 * @param request, see JsonObject.RequestTags[0], just query parameters
	 * <code>request Schema:{resp.ip,resp.port,parameters}</code>
	 * @return
	 * <code>response Schema:{resptopic,content}</code>
	 */
	private String handleRequest_internal(String request)
	{
		JsonNode jsnode = jsonParser.fromString(request);
		Map reqparamap = jsonParser.JsonNode2Map(jsnode);
//		Request spatial_request = HttpRequestParser.paserHttpRequestParameters(reqparamap);
		Request spatial_request = null;
		try {
			spatial_request = new Request(request);
		} catch (ParseException e1) {
			logger.error(e1);
		}
		
		DataRequestHandler request_handler = 
				RequestHandlerManager.getDataRequestHandler(spatial_request);
		String response_string = null;
		try {
			response_string = request_handler.handleRequest(spatial_request);
		} catch (Exception e) {
			logger.error(e);
		}
		return response_string;

	}

	@Override
	public void run() {
		String requestparas = request_paras;
		String response = HandleRequest(requestparas);
//		logger.info("response:" + response);
		logger.info("send response...");
//		ObjectNode jsnode = jsonParser.newObjectNode();
//		jsnode.put(JsonObject.ResponseTags[0],resptopic);
//		jsnode.put(JsonObject.ResponseTags[1], response);
		response_sender.sendResonse(resptopic,response,LocalConfig.EnableCompression);
	}

}
